home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.ada,comp.lang.c++,comp.lang.c,comp.lang.modula3,comp.lang.modula2
- Subject: Re: Hungarian notation - whoops!
- Date: Mon, 12 Feb 96 16:47:04 GMT
- Organization: none
- Message-ID: <824143624snz@genesis.demon.co.uk>
- References: <4fms62$c0p@goanna.cs.rmit.EDU.AU>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4fms62$c0p@goanna.cs.rmit.EDU.AU>
- ok@goanna.cs.rmit.EDU.AU "Richard A. O'Keefe" writes:
-
- >I have done a paper design for a machine, but never a real machine.
- >Sign and magnitude makes multiplication and division easy (just XOR the
- >signs and operate on the absolute values). Comparison isn't any harder
- >than 2s magnitude. If you can do addition and subtraction with 2s
- >complement, you can certainly do it for sign and magnitude with only a
- >couple more gate delays.
-
- Sure it is not difficult to implement sign-magnitude integer operations
- but you would have to add extra CPU instructions in order to do so.
- 2's complement has the big advantage that it can be done using the same
- instructions in many cases as unsigned arithmetic. This is particular
- relevant to RISC archetectures where opcode space is valued for other
- purposes such as addressing more registers.
-
- ...
-
- >The underlying base is not something I usually have to think about.
- >Most of my C code would work just as well in base 2, 3, 10, or even
- >balanced ternary. But the fact that abs(x) may deliver a negative
- >number is something I have to live with the whole time. _That_ is
- >what I regard as silly. I don't particularly care for the fact that
- >there are representable numbers X, Y for which X % Y may overflow,
- >either.
-
- This is a non-issue. If you treat any expression resulting in 'the most
- negative 2's complement integer' as an illegal result as far as your
- code is concerned then you are back to the same situation as if you
- were using sign-magnitude. Consider:
-
- int x = -32767-1;
- int y = abs(x);
-
- in:
-
- 1. A 2's complement implementation with INT_MAX==32767 and INT_MIN==-32768
-
- x is set to -32768
- the 2nd expression results in undefined behaviour.
-
- 2. A sign-magnitude implementation with INT_MAX==32767 and INT_MIN==-32767
-
- the first expression results in undefined behaviour.
-
- So as you can see the overall effect is the same in both cases. The only
- issue I can see is whether in a particular case you'd want to use
- INT_MIN or -INT_MAX for bounds checking.
-
- I am aware of the rather wide distribution of this follow-up. However the
- issues here are applicable to other languages - I hope the C syntax
- is self-evident! :-)
-
- As far as I can see, the major "architectural
- >reason" these days is "compatibility with existing C". (A little
- >unfair, but not a lot unfair.)
-
- See above.
-
- >I think that Dijkstra was right when he said "the purpose of machines is
- >to execute our programs", and I call anything "silly" that forces me to
- >work around arithmetic nasties in my source code.
-
- Do you check every signed integer operation for overflow (barring the ones
- you can prove can't overflow)?
-
- Whether the answer is yes or no you are no worse off using 2's complement
- that sign-magnitude (but for different reasons).
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-